package com.arm.controller.admin.user;

import com.arm.controller.BaseController;
import com.arm.module.AdminMenu;
import com.arm.module.Role;
import com.arm.service.AdminUserService;
import com.arm.service.MenuService;
import com.arm.service.RoleService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Controller
@RequestMapping(value = "/admin/role")
public class AdminRoleController extends BaseController {
@Autowired
AdminUserService adminUserService;
  @Autowired
  MenuService menuService;
    @Autowired
    RoleService roleService;
    @PostMapping(value="getMenuListForRole")
    public String getMenuListForRole(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters(request);
            resultObj.setData(menuService.selectMenuListForRole(map));
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
    @RequestMapping(value="/role")
    public ModelAndView Role(HttpServletRequest request) {
        ModelAndView m = new ModelAndView("admin/role/role");
        Map<String,String> map =this.showRequestParameters(request);
        if(StringUtils.isNotBlank(map.get("tid"))){
            Role role = roleService.selectRole(map);
            m.addObject("role",role);
        }
        return  m;
    }


    @RequestMapping(value="/addRole",method = RequestMethod.POST)
    @ResponseBody
    public String addRole(HttpServletRequest request) {
        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters(request);
            roleService.addRole(map);
            if(map.get("menuIds")!=null){
              //  roleService.modifyRole(map);
            }
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }

    @RequestMapping(value="/saveMenuRole",method = RequestMethod.POST)
    @ResponseBody
    public String saveMenuRole(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setMsg("成功！");
           // roleService.modifyRole(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
  @RequestMapping(value="/deleteMenu", method = RequestMethod.POST)
    @ResponseBody
    public String deleteMenu(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setMsg("成功！");
            menuService.deleteMenu(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }@RequestMapping(value="/addMenu", method = RequestMethod.POST)
    @ResponseBody
    public String addMenu(AdminMenu adminMenu) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            resultObj.setMsg("成功！");
            menuService.addMenu(adminMenu);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }


    @RequestMapping(value="/getRoleList")
    public ModelAndView getUserList(HttpServletRequest request) {
        System.out.println("=============================getRoleList=============================================>>>>>>");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("admin/role/getRoleList");

        Map<String,String> map=this.showRequestParameters(request);
        List<Role> roleList = roleService.getRoleList(map);
        mov.addObject("bean",map);
        mov.addObject("roleList",roleList);
        return mov;

    }





}
